/*
 * Copyright (c) 2024 Huawei Technologies Co., Ltd.
 * This program is free software, you can redistribute it and/or modify it under the terms and conditions of
 * CANN Open Software License Agreement Version 2.0 (the "License").
 * Please refer to the License for details. You may not use this file except in compliance with the License.
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
 * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
 * See LICENSE in the root of the software repository for the full text of the License.
 */
#include <mki/base/kernel_base.h>
#include <mki_loader/op_register.h>
#include <mki/utils/log/log.h>
#include <mki/utils/math/tensor_utils.h>
#include <mki/utils/math/math.h>
#include "asdops/params/transdata.h"
#include "kernels/transdata/tiling/transdata_tiling.h"

namespace AsdOps {
// Transdata
class TransdataKernel : public KernelBase {
public:
    explicit TransdataKernel(const std::string &kernelName, const BinHandle *handle) noexcept
        : KernelBase(kernelName, handle)
    {
    }

    bool CanSupport(const LaunchParam &launchParam) const override
    {
        MKI_CHECK(launchParam.GetParam().Type() == typeid(OpParam::Transdata),
            "transdata: param type invalid", return false);
        MKI_CHECK(launchParam.GetInTensorCount() == 1, "input num invalid", return false);
        MKI_CHECK(launchParam.GetOutTensorCount() == 1, "output num invalid", return false);
        return true;
    }

    Status InitImpl(const LaunchParam &launchParam) override
    {
        return TransdataCommonTiling(GetName(), launchParam, kernelInfo_, *GetBinHandle());
    }
};

// TransdataNzToNdKernel
class TransdataNzToNdKernel : public TransdataKernel {
public:
    explicit TransdataNzToNdKernel(const std::string &kernelName, const BinHandle *handle) noexcept
        : TransdataKernel(kernelName, handle)
    {
    }

    bool CanSupport(const LaunchParam &launchParam) const override
    {
        MKI_CHECK(TransdataKernel::CanSupport(launchParam), "failed to check support", return false);
        MKI_CHECK(launchParam.GetInTensor(0).desc.dtype == TENSOR_DTYPE_FLOAT16 ||
            launchParam.GetInTensor(0).desc.dtype == TENSOR_DTYPE_BF16,
            "tensor dtype unsupported", return false);
        MKI_CHECK(launchParam.GetInTensor(0).desc.format == TENSOR_FORMAT_FRACTAL_NZ,
            "tensor format unsupported", return false);
        return true;
    }
};
REG_KERNEL_BASE(TransdataNzToNdKernel);

// TransdataNdToNzKernel
class TransdataNdToNzKernel : public TransdataKernel {
public:
    explicit TransdataNdToNzKernel(const std::string &kernelName, const BinHandle *handle) noexcept
        : TransdataKernel(kernelName, handle)
    {
    }

    bool CanSupport(const LaunchParam &launchParam) const override
    {
        MKI_CHECK(TransdataKernel::CanSupport(launchParam), "failed to check support", return false);
        MKI_CHECK(launchParam.GetInTensor(0).desc.dtype == TENSOR_DTYPE_FLOAT16 ||
            launchParam.GetInTensor(0).desc.dtype == TENSOR_DTYPE_BF16,
            "tensor dtype unsupported", return false);
        MKI_CHECK(launchParam.GetInTensor(0).desc.format == TENSOR_FORMAT_ND,
            "tensor format unsupported", return false);
        return true;
    }
};
REG_KERNEL_BASE(TransdataNdToNzKernel);

// TransdataNdToNzInt8Kernel
class TransdataNdToNzInt8Kernel : public TransdataKernel {
public:
    explicit TransdataNdToNzInt8Kernel(const std::string &kernelName, const BinHandle *handle) noexcept
        : TransdataKernel(kernelName, handle)
    {
    }

    bool CanSupport(const LaunchParam &launchParam) const override
    {
        MKI_CHECK(TransdataKernel::CanSupport(launchParam), "failed to check support", return false);
        MKI_CHECK(launchParam.GetInTensor(0).desc.dtype == TENSOR_DTYPE_INT8,
            "tensor dtype unsupported", return false);
        MKI_CHECK(launchParam.GetInTensor(0).desc.format == TENSOR_FORMAT_ND,
            "tensor format unsupported", return false);
        return true;
    }
};
REG_KERNEL_BASE(TransdataNdToNzInt8Kernel);
} // namespace AsdOps